from celery_tasks.main import app
from meiduo_mall.libs.yuntongxun.sms import CCP


@app.task(bind=True, name='send_sms_code')
def send_sms_code(self, mobile, sms_code, time):
    # 1.发送短信
    try:
        ccp = CCP()
        result = ccp.send_template_sms(mobile, [sms_code, time], 1)  # 结果只有0或-1,有可能发成功,对方没有收到
    except Exception as e:
        result = -1

        # 2.判断短信是否发送成功
        if result == -1:
            print('发送失败!')
            # exc最终发送失败(重试的次数到了)之后报什么异常,countdown隔5秒发一次,max_retries重新发送3次
            self.retry(exc=Exception('最终没有发送成功'), countdown=5, max_retries=3)  # 此处self代表celery的对象,retry重试
